package com.seal.commons.generator.config;

import com.seal.commons.generator.enums.DbType;
import lombok.Data;
import lombok.experimental.Accessors;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 功能描述: <br/>
 *
 * @author yong.huang
 * @date: 2020-08-02 11:18<br/>
 * @since JDK 1.8
 */
@Data
@Accessors(chain = true)
public class DatabaseConfig {

    /**
     * 数据库类型
     */
    private DbType dbType;

    /**
     * 驱动连接的URL
     */
    private String url;
    /**
     * 驱动名称
     */
    private String driverName;
    /**
     * 数据库连接用户名
     */
    private String username;
    /**
     * 数据库连接密码
     */
    private String password;


    /**
     * 创建数据库连接对象
     *
     * @return Connection
     */
    public Connection getConn() {
        Connection conn;
        try {
            Class.forName(driverName);
            conn = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
        return conn;
    }


}